/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-07
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.*;
import com.rzico.basics.service.*;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 全场活动控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-07
 */
@Api(description = "全场活动控制层")
@RestController
@RequestMapping("/promotion")
public class PromotionController extends BaseController {

    @Autowired
    private PromotionService promotionService;

    @Autowired
    private MemberPromotionService memberPromotionService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SysSequenceService sysSequenceService;

    @Autowired
    private PromotionGiftService promotionGiftService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 获取首页弹窗活动
     *
     * @return
    */
    @ApiOperation("获取首页弹窗活动")
    @GetMapping("/popScreen")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query")
    })
    public CommResult<Promotion> popScreen(String mchId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        params.put("type", 0);
        params.put("status", 1);
        params.put("deleted", 0);
        Member member = memberService.getCurrent();
        if (member!=null) {
            params.put("memberPromotion", true);
            params.put("memberId", member.getId());
        }
        List<Promotion> list = promotionService.selectList(params);
        for (Promotion promotion:list)  {
            Map<String,Object> data = new HashMap<>();
            data.put("promotion",promotion);
            if (promotion.getGiftType().equals(0)) {
                data.put("itemList",promotionGiftService.getProductGift(promotion.getId()));
            } else {
                data.put("itemList",promotionGiftService.getCouponGift(promotion.getId()));
            }
            return CommResult.success(data);
        }
        return CommResult.success("无活动");
    }


    /**
     * 获取扫码活动
     *
     * @return
     */
    @ApiOperation("获取扫码活动")
    @GetMapping("/scaner")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query")
    })
    public CommResult<Promotion> scaner(String mchId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        params.put("type", 2);
        params.put("status", 1);
        params.put("expired", false);
        params.put("deleted", 0);

        List<Promotion> list = promotionService.selectList(params);
        for (Promotion promotion:list)  {
            Map<String,Object> data = new HashMap<>();
            data.put("promotion",promotion);
            if (promotion.getGiftType().equals(0)) {
                data.put("itemList",promotionGiftService.getProductGift(promotion.getId()));
            } else {
                data.put("itemList",promotionGiftService.getCouponGift(promotion.getId()));
            }
            return CommResult.success(data);
        }
        return CommResult.success("无活动");
    }

    /**
     * 点击领取
     *
     * @param promotionId
     * @return
     */
    @ApiOperation("点击领取")
    @PostMapping("/clickAndGet")
    public CommResult<Qrcode> clickAndGet(Long promotionId) {
        Member member = memberService.getCurrent();

        if (member==null) {
            return CommResult.logout();
        }

        if (memberPromotionService.checkExists(promotionId,member.getId())>0) {
            return CommResult.error("您已经领取了");
        }

        Promotion promotion = promotionService.selectByPrimaryKey(promotionId);
        if (promotion!=null) {
            if (promotion.getGiftType().equals(0)) {
                return CommResult.error("此功能未开通");
            }
            if (promotion.getFirsted()!=null && promotion.getFirsted()==true) {
                if (member.getAmount().compareTo(BigDecimal.ZERO)>0) {
                    return CommResult.error("新用户才能参与");
                }
            }
            promotionService.createGift(promotionId,member,sysSequenceService.generate("couponCode"));
            return CommResult.success("领取成功");
        } else {
            return CommResult.success("手慢了,已结束");

        }

    }


}
